<?
session_start();
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';


$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
$twitterObj->setToken($_GET['oauth_token']);
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
$twitterInfo= $twitterObj->get_accountVerify_credentials();


header( "Content-Type: text/html; Charset=utf-8" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );

foreach( $_GET as $Key => $Value ) {
	$_GET[$Key] = str_replace("\\\\", "\\", $Value );
	$_GET[$Key] = str_replace("\\'", "'", $_GET[$Key] );
	$_GET[$Key] = str_replace("\\\"", "\"", $_GET[$Key] );
}


//*****************************************************************
//SESSION
//*****************************************************************
$_SESSION['twitter_username'] = $twitterInfo->screen_name;
$_SESSION['twitter_icon'] = $twitterInfo->profile_image_url;

$_SESSION['twitterObj'] = new EpiTwitter($consumer_key, $consumer_secret);
$_SESSION['token'] = $twitterObj->getAccessToken();
$_SESSION['twitterInfo'] = $twitterObj->get_accountVerify_credentials();



//*****************************************************************************
//DB
//*****************************************************************************
$Server = 'PC-025';
$DbName = 'circle_manager';
$User = 'root';
$Password = 'password';

//*****************************************************************************
//接続処理
//*****************************************************************************
$Connect = @mysql_connect( $Server, $User, $Password );
if ( !$Connect ) {
	print "接続エラーです";
	exit();
}
//*****************************************************************************
//DB選択
//*****************************************************************************
mysql_select_db( $DbName, $Connect );
mysql_query("set names 'utf8'", $Connect);

//*****************************************************************************
//クエリ
//*****************************************************************************
$result = mysql_query("select * from user where twitter_username = '{$_SESSION['twitter_username']}'", $Connect);

if ( !$result ) {
	print "\n";
	print "<span style='color:#f00'>" . mysql_error() . "</span>";
}
//*****************************************************************************
//列数
//*****************************************************************************
$nField = @mysql_num_fields( $result );
if ( $nField ) {
	$nCount = 0;
	while ($row = mysql_fetch_row($result)) {
		for( $i = 0; $i < $nField; $i++ ) {
		}
		$nCount++;
	}
}

//*****************************************************************************
//接続解除
//*****************************************************************************
mysql_close($Connect);


//*****************************************************************************
//分岐処理
//*****************************************************************************
if( mysql_num_rows($result) > 0 ){
	header("location: http://localhost/sinkyu/twitter_0703/goal.php");
	exit;
}
else{
	header("location: http://localhost/sinkyu/twitter_0703/new.php");
	exit;
}


?>